//
//  FLiewController.m
//  WeiDuKeJi
//
//  Created by 谷银行 on 2019/1/26.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "FLiewController.h"
#import "FlieModel.h"
@interface FLiewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UICollectionView *coll;
}
@property (nonatomic , copy)NSArray *Array;
@end

@implementation FLiewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CollView];
    [self LoadData];
}
- (void)CollView{
    UICollectionViewFlowLayout *fl=[[UICollectionViewFlowLayout alloc] init];
    fl.minimumLineSpacing=10;
    fl.minimumInteritemSpacing=10;
    fl.scrollDirection = UICollectionViewScrollDirectionVertical;
    fl.itemSize=CGSizeMake(190, 190);
    fl.sectionInset=UIEdgeInsetsMake(10, 10, 0, 10);
    
    coll =[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:fl];
    coll.delegate=self;
    coll.dataSource=self;
    coll.backgroundColor=[UIColor whiteColor];
    [coll registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:coll];
}
- (void)LoadData{
    
    self.Array = [[NSMutableArray alloc]init];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    
    [manager GET:@"https://172.17.8.100/techApi/information/v1/findAllInfoPlate" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        FlieModel *model = [FlieModel yy_modelWithJSON:responseObject];
        self.Array = model.result;
        [coll reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.Array.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    RArrModel *model = self.Array[indexPath.row];
    UIImageView *img=[[UIImageView alloc] init];
    [cell addSubview:img];
    img.sd_layout
    .topSpaceToView(self, 20)
    .leftSpaceToView(self, 0)
    .heightIs(190)
    .widthIs(190);
    NSURL *url = [NSURL URLWithString:model.pic];
    NSData *data = [NSData dataWithContentsOfURL:url];
    img.image = [UIImage imageWithData:data];
    img.layer.masksToBounds=YES;
    img.layer.cornerRadius=10;
    
    UILabel *label = [[UILabel alloc]init];
    [img addSubview:label];
    label.sd_layout
    .topSpaceToView(img, 10)
    .leftSpaceToView(img, 10)
    .heightIs(30)
    .widthIs(150);
    label.text = model.name;
    label.textColor = [UIColor whiteColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RArrModel *model = self.Array[indexPath.row];
    if (self.plateIDBlock) {
        self.plateIDBlock(model.plateID);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
